package com.zb.mvc.filter;


import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.commons.lang3.StringUtils;

public class CharacterEncodingFilter
  implements Filter
{
  private final String DEFAULT_ENCODING = "UTF-8";
  
  private String encoding;
  
  private final String INITPARAM_ENCODING = "encoding";
  
  private final String CONTENT_TYPE = "application/json;charset=";
  
  public void destroy()
  {
    this.encoding = "UTF-8";
  }
  
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
    throws IOException, ServletException
  {
    request.setCharacterEncoding(this.encoding);
    response.setContentType("application/json;charset=" + StringUtils.lowerCase(this.encoding));
    response.setCharacterEncoding(this.encoding);
    filterChain.doFilter(request, response);
  }
  
  public void init(FilterConfig config)
    throws ServletException
  {
    this.encoding = config.getInitParameter("encoding");
    
    if (StringUtils.isBlank(this.encoding))
    {
      getClass();this.encoding = "UTF-8";
    }
  }
  
  public void setEncoding(String encoding)
  {
    this.encoding = encoding;
  }
}

